package com.example.spring.multi.communication;

import io.vertx.core.*;
import io.vertx.core.eventbus.EventBusOptions;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : <a href="https://iiiii.run">xiaohan.yuan</a>
 * @version :    1.0
 * @description :  初始化Vert.x
 * @date :    2021-09-10 18:30
 */
@Configuration
public class VertxInit {
    private final List<AbstractVerticle> verticleList;

    public VertxInit(List<AbstractVerticle> verticleList) {
        this.verticleList = verticleList;
    }

    @EventListener
    public void deployVerticles(ApplicationReadyEvent event) {
        EventBusOptions eventBusOptions = new EventBusOptions();
        VertxOptions vertxOptions = new VertxOptions();
        Vertx vertx = Vertx.vertx(vertxOptions);
        List<Future> futures = new ArrayList<>();
        for (AbstractVerticle abstractVerticle : verticleList) {
            futures.add(Future.<String>future(data -> vertx.deployVerticle(abstractVerticle, new DeploymentOptions().setInstances(1), data)));
        }
        CompositeFuture.all(futures).onSuccess(composite->{
            if (composite.succeeded()){
                System.out.println(composite.result());
            }else {
                System.out.println(composite.causes());
            }
        });
    }
}
